package com.douwong.student.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.douwong.student.R;
import com.douwong.student.adapter.TestRecordPageAdapter;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.base.BaseTestRecordChoiceFragment;
import com.douwong.student.fragment.TestRecordFillFragment;
import com.douwong.student.fragment.TestRecordJudgeFragment;
import com.douwong.student.model.ExamResult;
import com.douwong.student.model.HallTestData;
import com.douwong.student.model.HallTestModel;
import com.douwong.student.model.LessExamine;
import com.douwong.student.model.ListPlExamineItemBean;
import com.douwong.student.model.Qditem;
import com.douwong.student.utils.ToastUtils;
import com.douwong.student.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import static com.douwong.student.R.id.ll_Title_Page;
import static com.douwong.student.R.id.tv_Title_pic;
import static com.douwong.student.R.id.tv_total_score;
import static com.douwong.student.R.id.viewpage;

/**
 * 显示课堂考试记录
 */
public class ShowClassExamActivity extends BaseActivity {
    private LinearLayout mLlPagePoint;

    private TextView mTvTotalScore,mTvTitlePic;
    private ViewPager mViewPager;
    private ArrayList<ListPlExamineItemBean> mLesLessonExamineItemList;//
    private LessExamine mLessonExamine;//
    private List<BaseFragment> mBaseFragmentList;
    private TestRecordPageAdapter mTestRecordPageAdapter;
    private TextView mTvlastAnswer;
    private ArrayList<ExamResult> mYesOrNo;
    private ImageView mIvPeople;

    public static void startActivity(Context context,String content,String exResult,String exYesNo){
        Intent intent=new Intent(context,ShowClassExamActivity.class);
        intent.putExtra("json", content);
        intent.putExtra("exResult", exResult);
        intent.putExtra("exYesNo", exYesNo);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_show_class_exam);
        mLlPagePoint = (LinearLayout) findViewById(ll_Title_Page);
        mTvTotalScore = (TextView) findViewById(tv_total_score);
        mTvTitlePic = (TextView) findViewById(tv_Title_pic);
        mViewPager = (ViewPager) findViewById(viewpage);
        mTvlastAnswer = (TextView) findViewById(R.id.tv_lastAnswer);
        mIvPeople = (ImageView) findViewById(R.id.iv_people);
    }

    @Override
    protected void initData() {
        super.initData();
        String json = getIntent().getStringExtra("json");
        HallTestModel hallTestModel = JSON.parseObject(json, HallTestModel.class);
        mYesOrNo = (ArrayList<ExamResult>) JSON.parseArray(getIntent().getStringExtra("exYesNo"), ExamResult.class);
        if (hallTestModel!=null){
            HallTestData hallTestData = hallTestModel.getData();
            mLesLessonExamineItemList = hallTestData.getLesLessonExamineItemList();
            mLessonExamine = hallTestData.getLessonExamine();
            mTvTotalScore.setText(mLessonExamine.getExScore() + "分");
            itemNumberDisplay(mLesLessonExamineItemList.size());
            displayClassfy(mLesLessonExamineItemList);
            mTestRecordPageAdapter = new TestRecordPageAdapter(getSupportFragmentManager(),mBaseFragmentList);
            mViewPager.setAdapter(mTestRecordPageAdapter);
            mViewPager.setOffscreenPageLimit(mLesLessonExamineItemList.size());
            showItem(0);

        }else {

            ToastUtils.makeText(this,"数据解析错误");
            finish();
        }


    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {


                showItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置选中的条目 和设置答案
     * @param currentPage
     */
    private void showItem(int currentPage){
        showResult(currentPage);
        mTvTitlePic.setBackgroundDrawable(setTitlePicByTitle( mLesLessonExamineItemList.get(currentPage).getQdItem().getItemType()));
        mViewPager.setCurrentItem(currentPage);
        for(int j=0;j<mLesLessonExamineItemList.size();j++){
            mLlPagePoint.getChildAt(j).setBackgroundResource(R.mipmap.not_to_do);
        }
        mLlPagePoint.getChildAt(currentPage).setBackgroundResource(R.mipmap.used_do);

    }

    /**
     * 题目类型设置数据
     * @param tileType
     * @return
     */
    private Drawable setTitlePicByTitle(int tileType) {
        Drawable drawable = null;
        if (tileType==1) {// 单选
            drawable = getResources().getDrawable(R.mipmap.radiobuttonlistpic);
        } else if (tileType==2) {// 多选
            drawable = getResources().getDrawable(R.mipmap.multiple_choice_question_pic);
        } else if (tileType==15) {// 判断题
            drawable = getResources().getDrawable(R.mipmap.judgment_questions_pic);
        } else if (tileType==3) {// 填空
            drawable = getResources().getDrawable(R.mipmap.fil_the_blanks_pic);
        }
        return drawable;
    }

    /**
     * 显示答案
     * @param i
     */
    private void showResult(int i){

        if(mYesOrNo!=null){
            logcatI("mYesOrNo  ： "+mYesOrNo.toString());
            int j=mYesOrNo.get(i).getIsWaiver();
            if(j==1){//弃权
                mIvPeople.setBackgroundResource(R.mipmap.answerwrong_people);
            }else if(j==0){
                if(mYesOrNo.get(i).getYesOrNo()==1){//回答正确
                    mIvPeople.setBackgroundResource(R.mipmap.answerright);
                }else if(mYesOrNo.get(i).getYesOrNo()==0){//回答错误
                    mIvPeople.setBackgroundResource(R.mipmap.answerwrong_people);
                }
            }
        }else{
            mIvPeople.setBackgroundResource(R.mipmap.answerwrong_people);
        }
        Qditem qditem = mLesLessonExamineItemList.get(i).getQdItem();
        String rightAnswer="";
        switch (qditem.getItemType()) {
            case 1://单选
                rightAnswer=qditem.getAnswers().replace("*#*","");
                break;
            case 2://	多选
                rightAnswer = qditem.getAnswers().replace("*#*", " ");
                break;
            case 3://填空
                rightAnswer=qditem.getAnswers().replace("*#*", "  ");
                break;
            case 15://判断
                if(qditem.getAnswers().equals("A")){
                    rightAnswer="正确";
                }else if(qditem.getAnswers().equals("B")){
                    rightAnswer="错误";
                }

        }
        mTvlastAnswer.setText("正确答案:"+(rightAnswer==null?"":rightAnswer));

    }

    /**
     * 显示数据分类
     * @param lesLessonExamineItemList
     */
    private void displayClassfy(ArrayList<ListPlExamineItemBean> lesLessonExamineItemList) {
        mBaseFragmentList = new ArrayList<>();
        BaseFragment baseFragment=null;

        for (int i = 0; i < lesLessonExamineItemList.size(); i++) {
            switch (lesLessonExamineItemList.get(i).getQdItem().getItemType()) {
                case 1:
                 //单选
                    baseFragment =new BaseTestRecordChoiceFragment(lesLessonExamineItemList.get(i))   ;

                    break;
                case 2:
                   //多选
                   baseFragment =new BaseTestRecordChoiceFragment(lesLessonExamineItemList.get(i))   ;
                   break;
                  case 3:
                    //填空题
                    baseFragment=new TestRecordFillFragment(lesLessonExamineItemList.get(i));
                    break;
                case 15:
                    //判断题
                    baseFragment=new TestRecordJudgeFragment(lesLessonExamineItemList.get(i));
                    break;
              default:
                    baseFragment =new BaseFragment() {
                        @Override
                        protected View getRootView(LayoutInflater inflater, ViewGroup container) {
                          TextView textView=  new TextView(UIUtils.getContext());
                            textView.setText("空数据");
                            return textView;
                        }
                    };
                    break;
            }
            baseFragment.setExamResultList(mYesOrNo);
            mBaseFragmentList.add(baseFragment);

        }
    }

    /**
     * 下方页数控件显示
     * @param itemSize
     */
    public void itemNumberDisplay(int itemSize) {
        for (int i = 0; i < itemSize; i++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( UIUtils.getDimens(R.dimen.dp_65), UIUtils.getDimens(R.dimen.dp_70));
            lp.leftMargin = 15;


            final TextView textView=new TextView(ShowClassExamActivity.this);
            if(i==0){
                textView.setBackgroundResource(R.mipmap.used_do);
            }else{
                textView.setBackgroundResource(R.mipmap.not_to_do);
            }
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER);
            textView.setText((i+1)+"");
            textView.setTextSize(UIUtils.getDimens(R.dimen.sp25));
            mLlPagePoint.addView(textView);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   int slectPoint=Integer.parseInt(((TextView)v).getText().toString())-1;
                    showItem(slectPoint);

                }
            });
        }
    }
}
